//自动分配的固定大小的块内存池
//通过ABMemoryPool_create创建一个内存池
//每次申请一个固定大小的内存,没有空闲块时向系统申请,有空闲内存块时分配空闲内存块
//释放时把内存块放到空闲内存链表
//手动释放空闲内存，并保留一定的空闲内存块

#ifndef RTSP_SERVICE_ABMEMORYPOOL_H
#define RTSP_SERVICE_ABMEMORYPOOL_H

typedef struct ABMemoryBlock {
    struct ABMemoryBlock *next_ptr;      //下一块内存指针
    struct ABMemoryBlock *last_ptr;      //上一块内存指针
} ABMemoryBlock;

typedef struct ABMemoryPool {
    unsigned int block_size;                    //内存块大小
    unsigned int leisure_number;                //空闲块的数量,用于自动释放空闲内存
    struct ABMemoryBlock *leisure_prt;          //空闲的块的指针,队列的出
    struct ABMemoryBlock *practical_ptr;        //使用的块的指针
} ABMemoryPool;

//内存池创建,  block_size：内存块大小
extern void ABMemoryPool_create(ABMemoryPool *pool, unsigned int block_size);

//从内存池申请内存
extern void *ABMemoryPool_block_malloc(ABMemoryPool *pool);

//从内存池释放内存
extern void ABMemoryPool_block_free(ABMemoryPool *pool, void *ptr);

//释放空闲的内存块, leisure_max_number:保留的空闲内存块数量
extern void ABMemoryPool_free_leisure(ABMemoryPool *pool, unsigned int leisure_max_number);

//内存池销毁
extern void ABMemoryPool_destroy(ABMemoryPool *pool);

#endif //RTSP_SERVICE_ABMEMORYPOOL_H
